<?php

namespace app\api\controller\mall;

use think\Db;

class Question extends CommonMall {
	public function index()
	{
		$category = $this->db->name('mall_question_category')->order('sort desc,id asc')->column('id,title');
		$question = $this->db->name('mall_question')->order('sort desc, id desc')->field('id,title,category')->select();
		$data = ['全部'=>$question];
		$datalist = [];
		foreach ($category as $k => $v) {
			foreach ($question as $key => $value) {
				if ($k == $value['category']) {
					if (!isset($data[$v])) {
						$data[$v] = [];
					}
					$data[$v][] = $value;
				}
			}
		}
		foreach ($data as $k => $v) {
			$datalist[] = [
				'title' => $k,
				'list'	=> $v
			];
		}
		return $this->pass($datalist);
	}

	public function detail($id)
	{
		$data = $this->db->name('mall_question')->field('id,title,content')->find($id);
		if (!$data) {
			return $this->miss('内容不存在');
		}
		return $this->pass($data);
	}
}